// DFS 
class Solution {
public:
    int maxMoves(vector<vector<int>>& grid) {
        int n = grid.size(), m = grid[0].size();
        int ans = 0;
        auto dfs = [&](this auto&& dfs, int x, int y){
            ans = max(ans, y);
            if(ans == m - 1) return;
            
            for(int k = max(0, x - 1); k < min(x + 2, n); ++k)
            {
                if(grid[k][y + 1] > grid[x][y])
                    dfs(k, y + 1);
            }

            grid[x][y] = 0;
        };

        for(int i = 0; i < n; ++i)
        {
            dfs(i, 0);
        }
        return ans;
    }
};

